package com.link.play.fit.ai.ui.vip

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.android.billingclient.api.ProductDetails
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.databinding.AcVipBuyLayoutBinding
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.utils.GooglePayUtil
import com.link.play.fit.ai.utils.GooglePayUtil.extractAndCalculateDailyPriceToMonth
import kotlinx.coroutines.launch

class VipBuyAc : BaseActivity<AcVipBuyLayoutBinding>(AcVipBuyLayoutBinding::inflate) {

    private var yearSubscriptionOfferDetails: List<ProductDetails.SubscriptionOfferDetails>? = null
    private var yearProductDetails: ProductDetails? = null
    private var yearFormattedPrice = ""

    private var monthSubscriptionOfferDetails: List<ProductDetails.SubscriptionOfferDetails>? = null
    private var monthProductDetails: ProductDetails? = null
    private var monthFormattedPrice = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initListener()
        initData()
    }

    private fun initData() {

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun initListener() {
        binding.subscroptionText.setOnClickListener {
            if(!binding.vipSwitch.isChecked){
                yearSubscriptionOfferDetails?.forEach {
//                    if (it.basePlanId == SUB_YEAR_PLAN_ID) {
//                        if(it.pricingPhases.pricingPhaseList[0].formattedPrice=="免费"){
//                            "offerToken  ${it.offerToken}".d()
//                            GooglePayUtil.pay(this, it.offerToken, yearProductDetails!!)
//                        }
//                    }
                }
            }else{
//                monthSubscriptionOfferDetails?.forEach {
//                    if (it.basePlanId == SUB_MONTH_PLAN_ID) {
//                        GooglePayUtil.pay(this, it.offerToken, monthProductDetails!!)
//                    }
//                }
            }
        }
        binding.vipSwitch.setOnCheckedChangeListener { _, b ->
            if(b){
                binding.yearLayout.background = getDrawable(R.drawable.bg_style_gray_r5)
                binding.weekLayout.background = getDrawable(R.drawable.bg_vip_style_chose)
            }else{
                binding.yearLayout.background = getDrawable(R.drawable.bg_vip_style_chose)
                binding.weekLayout.background = getDrawable(R.drawable.bg_style_gray_r5)
            }
        }
        binding.yearLayout.setOnClickListener {
            binding.vipSwitch.isChecked = false
        }
        binding.weekLayout.setOnClickListener {
            binding.vipSwitch.isChecked = true

        }
        binding.subscroptionText.setOnClickListener {

        }
        lifecycleScope.launch {
//            yearProductDetails = GooglePayUtil.queryProduct(SUB_YEAR_product_ID)
            yearSubscriptionOfferDetails = yearProductDetails?.subscriptionOfferDetails
            yearProductDetails?.subscriptionOfferDetails?.forEach {
                val pricingPhase = it.pricingPhases.pricingPhaseList[0]
                "it.basePlanId  ${it.basePlanId}".d()

                when (it.basePlanId) {

//                    SUB_YEAR_PLAN_ID -> {
//                        yearFormattedPrice = pricingPhase.formattedPrice
//                        binding.yearlyValueText.text = getString(
//                            R.string.yearly_mo,yearFormattedPrice
//                        )
//                        binding.yearlyAverageValueText.text = getString(
//                            R.string.yearly_mo,
//                            extractAndCalculateDailyPriceToMonth(yearFormattedPrice)
//                        )
//                    }
                }
            }
        }

        lifecycleScope.launch {
//            monthProductDetails = GooglePayUtil.queryProduct(SUB_MONTH_PRODUCT_ID)
//            monthSubscriptionOfferDetails = monthProductDetails?.subscriptionOfferDetails
//            monthProductDetails?.subscriptionOfferDetails?.forEach {
//                val pricingPhase = it.pricingPhases.pricingPhaseList[0]
//
//                "monthProductDetails.formattedPrice ${ pricingPhase.formattedPrice} priceAmountMicros ${ pricingPhase.priceAmountMicros}".d()
//                when (it.basePlanId) {
//                    SUB_MONTH_PLAN_ID -> {
//                        monthFormattedPrice = pricingPhase.formattedPrice
//                        binding.weeklyValueText.text = getString(
//                            R.string.weekly_price,
//                            monthFormattedPrice
//                        )
//                    }
//                }
//            }
        }
    }
}